package com.zx.oa.controller;

import com.alibaba.excel.EasyExcel;
import com.zx.oa.dao.ClassInfo.ClassInfoDao;
import com.zx.oa.dao.ClassInfo.ClassInfoDaoImpl;
import com.zx.oa.dao.census.CensusDao;
import com.zx.oa.dao.census.CensusDaolpml;
import com.zx.oa.dao.morningResult.MorningResultDao;
import com.zx.oa.dao.morningResult.MorningResultDaoImpl;
import com.zx.oa.entity.CheckJob;
import com.zx.oa.entity.Major;
import com.zx.oa.entity.Result;
import com.zx.oa.entity.WeekResult;
import com.zx.oa.service.morningResult.MorningResultService;
import com.zx.oa.service.morningResult.MorningResultServiceImpl;
import com.zx.oa.service.weekResult.WeekResultService;
import com.zx.oa.service.weekResult.WeekResultServiceImpl;
import com.zx.oa.utils.PageSupport;
import com.zx.oa.utils.excel.ResultExcel;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet("/MorningResultServlet.do")
public class MorningResultServlet extends HttpServlet {
    MorningResultService morningResultService=new MorningResultServiceImpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String caozuo = request.getParameter("param");
        if ("cha".equals(caozuo)) {
            chaMorningResult(request, response);
        }else if ("delete".equals(caozuo)){
            deleteMorningResult(request,response);
        }else if ("excel".equals(caozuo)){
            excelResult(request,response);
        }else if ("excelMonth".equals(caozuo)){
            excelMonthResult(request,response);
        }else if ("modify".equals(caozuo)){
            modifyResult(request,response);
        }
    }

    private void modifyResult(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String date = request.getParameter("date");
        String result = request.getParameter("result");
        WeekResultService weekResultService = new WeekResultServiceImpl();
        boolean flag=weekResultService.modifyWeekResult(date, result, id);
//        morningResultService.modifyResult(id,date, result );
        if (flag){
            response.sendRedirect("MorningResultServlet.do?param=cha");
        }

    }


    private void excelMonthResult(HttpServletRequest request, HttpServletResponse response) {
        String emajor = request.getParameter("Emajor");
        String eclassname = request.getParameter("Eclassname");
        String month = request.getParameter("month");
        List<WeekResult> morningResultList=morningResultService.getMonthResultList(emajor,eclassname,month);

    }

    private void excelResult(HttpServletRequest request, HttpServletResponse response) throws IOException {
        SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //获取到时间，以时间来定义下载Excel的名称
        String downLoadTime=sf.format(new Date());

        List<WeekResult> morningResultList=(List<WeekResult>)request.getSession().getAttribute("morningResultList");
        //设置Excel响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //下载的Excel文件名称
        String fileName="MorningResult"+downLoadTime+".xlsx";
        response.setHeader("Content-disposition","allachment;filename="+fileName);
        EasyExcel.write(response.getOutputStream(),
                ResultExcel.class).sheet("晨考信息").doWrite(morningResultList);
    }

    private void deleteMorningResult(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        boolean flag=morningResultService.deleteResult(id);
        if (flag){
            response.sendRedirect("MorningResultServlet.do?param=cha");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doGet(req, resp);
    }


    private void chaMorningResult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String major=null;//查询的专业的名字
        String className=null;//查询的班级的名字
        String majorJsp=request.getParameter("major" );
        String classNameJsp=request.getParameter("classname" );
        if(null!=majorJsp&&!"".equals(majorJsp)){
            major=majorJsp;
        }
        if(null!=classNameJsp&&!"".equals(classNameJsp)){
            className=classNameJsp;
        }

        String studentName=null;//查询的学生的姓名
        String date=null;//查询的时间
        String result=null;
        String studentName1=request.getParameter("namec" );
        String date1=request.getParameter("date" );
        String result1=request.getParameter("result" );
        String month = request.getParameter("month");

        if(null!=studentName1&&!"".equals(studentName1)){
            studentName=studentName1;
        }
        if(null!=date1&&!"".equals(date1)){
            date=date1;
        }
        if(null!=result1&&!"".equals(result1)){
            result=result1;
        }

        String emajor = request.getParameter("Emajor");


        String pageIndex= request.getParameter("pageIndex");
        if(pageIndex==null){
            //第一次访问查询的servlet的时候没有pageIndex，赋默认值1
            pageIndex="1";
        }
        int currentPageNo=1;//当前页号
        int pageSize= PageSupport.pageSizeVal;//页面尺寸复制
        currentPageNo=Integer.parseInt(pageIndex);
        //存储页面信息
        PageSupport pages=new PageSupport();
        MorningResultDao morningResultDao = new MorningResultDaoImpl();
        int totalCount= morningResultDao.getMorningResultTotalCount(major,className,studentName,date,result);
        //设置页面尺寸
        pages.setPageSize(pageSize);
        //设置页面总记录数（注意此方法计算得到总页面数量）
        pages.setTotalCount(totalCount);
        //获取到总的页数
        int totalPageCount=pages.getTotalPageCount();
        if(currentPageNo<1){
            currentPageNo=1;
        }else if(currentPageNo>totalPageCount){
            currentPageNo=totalPageCount;
        }
        //将当前页号存储到pages对象
        pages.setCurrentPageNo(currentPageNo);
        ClassInfoDao classInfoService=new ClassInfoDaoImpl();
        List<WeekResult> morningResultList=morningResultService.getMorningResultList(major,className,studentName,date,result,currentPageNo,pageSize);
        CensusDao censusDaolpml = new CensusDaolpml();
        request. setAttribute("MajorAll",censusDaolpml.getMajorAll());
        request.getSession().setAttribute("morningResultList",morningResultList);
        request.setAttribute("classinfo",classInfoService.getClassInfoByStudyType(major));
        request.setAttribute("namec",studentName);
        request.setAttribute("date",date);
        request.setAttribute("pages",pages);
        request.setAttribute("Emajor",emajor);
        request.setAttribute("major",major);
        request.setAttribute("month",month);
        request.getRequestDispatcher("report/reportMorningResultInfo.jsp").forward(request,response);

    }
}